/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ca.jerg.config;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.prefs.BackingStoreException;
import java.util.prefs.InvalidPreferencesFormatException;
import java.util.prefs.Preferences;

/**
 *
 * @author dave
 */
public abstract class ErgConfig implements IErgConfig
{
	public static final String ERG_CFG_STR = "ErgCfg: ";
		
	public ErgConfig( String configData )
	{
		Preferences p = Preferences.userNodeForPackage(ErgConfigWork.class);
		config = p.node(configData);
	}
	
	public boolean importConfig( String path )
	{
		try
		{
			InputStream is = new BufferedInputStream(new FileInputStream(path));
			Preferences.importPreferences(is);
			is.close();
		}
		catch ( FileNotFoundException e )
		{
			System.err.println(ERG_CFG_STR + "Unable to locate file");
			return false;
		}
		catch ( IOException e )
		{
			System.err.println(ERG_CFG_STR + "IO error");
			return false;
		}
		catch ( InvalidPreferencesFormatException e )
		{
			System.err.println(ERG_CFG_STR + "Bad data file format");
			return false;
		}
		
		return true;
	}

	public boolean exportConfig( String path )
	{
		try
		{
			OutputStream os = new BufferedOutputStream(new FileOutputStream(path));
			config.exportSubtree(os);
			os.close();
		}
		catch ( FileNotFoundException e )
		{
			System.err.println(ERG_CFG_STR + "Unable to create file");
			return false;
		}
		catch ( IOException e )
		{
			System.err.println(ERG_CFG_STR + "IO error");
			return false;
		}
		catch ( BackingStoreException e )
		{
			System.err.println(ERG_CFG_STR + "Platform error");
			return false;
		}
		
		return true;
	}
	
	protected Preferences config;
}
